<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use User\Api\UserApi;
use Common\Api\BookGameApi;

class StudentController extends HomeController {

	/* 学生用户中心首页 */
	public function index(){
		
		$student     =  D('Student');
		$bookGame    =  $student->searchBookGame(2,1,session('user_auth.uid'));
		
		$rst         =  $student->searchBookGameDetail($bookGame,session('user_auth.uid'),session('user_auth.classid'));
		//dump($rst);
		//dump($rst[0]['student_game']);
		$this->assign('bookgame',$rst);
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		//dump(session('user_auth'));
		$this->display();
	}
	/* 
	 * 学生对单本书写笔记
	 *  
	 *  */
	public function writeNote(){
		if(IS_POST){
			//$map['id'] = I('post.id');
			$booklistGameRelation  =  D('BooklistGameRelation');
			if($booklistGameRelation->create()){
				$rst  =  $booklistGameRelation->save();
				if($rst){
					$this->success('保存笔记成功!',Cookie('__forward__'));
				}else{
					$this->error("保存笔记时报，请重试！");
				}
			}else{
				$this->error($booklistGameRelation->getError());
			}
		}else{
			$data['booklistid']   =  I('get.id');
			$data['bookname']     =  I('get.bookname');
			$booklistGameRelation =  M('BooklistGameRelation');
			$rst = $booklistGameRelation->find($data['booklistid']);
			$data['note']         =  $rst['note'];
			$this->ajaxReturn($data);
		}
		
	}
	/* 
	 * 答题
	 *  
	 *  */
	public function answerQuestion(){
		if(IS_POST){
			$map['correct_answer']  =  I('post.answers');
			$map['id']              =  I('post.id');
			$booklistGameRelation   =  M('BooklistGameRelation');
			if($booklistGameRelation->save($map)){
				$this->success("答题成功",U('index'));
			}else{
				$this->error("答题失败，请重试！");
			}
			
		}else{
			$map['bookid']  =  I('get.bookid');
			$bookTest       =  M('BookTest');
			$bookTestRst    =  $bookTest->where($map)->select();
			$this->ajaxReturn($bookTestRst,'JSON');
		}
		
		
	}
	
	/*
	 * 历史读书比赛
	 *   */
	public function nowBookGame(){
		$this->display();
	}
	/*
	 *  开始阅读
	 *   */
	public function startRead(){
		$student    =  D('Student');
		$bookgameid =  I('get.bookgameid');
		$booklistid =  I('get.id');
		if($student->startRead($bookgameid,$booklistid)){
			$this->success("开始读书成功！",Cookie('__forward__'));
		}else{
			$this->error($student->getError());
		}
	}
	/*
	 * 结束阅读
	 *   */
	public function endRead(){
		$student    =  D('Student');
		$studentid  =  session('user_auth.uid');
		$classid    =  session('user_auth.classid');
		$bookgameid =  I('get.bookgameid');
		$bookid     =  I('get.bookid');
		$booklistid =  I('get.id');
		if($student->endRead($bookgameid,$bookid,$studentid,$classid,$booklistid)){
			$this->success("结束读书成功！",Cookie('__forward__'));
		}else{
			$this->error($student->getError());
		}
	}
	/*
	 *  获得一场比赛中的班级信息
	 *   */
	public function getClass(){
		$bookGameId  = I('get.bookgameid');
		$bookGame    = new BookGameApi();
		$rst         = $bookGame->getClassInfo($bookGameId);
		/* $bookGame    = M('BookGame');
		$bookGameRst = $bookGame->find($bookGameId);
		$classids    = explode(',', $bookGameRst['classids']);
		$classInfo   = M('ClassInfo');
		$map['id']   = array('in',$classids);
		$rst = $classInfo->where($map)->select(); */
		$this->ajaxReturn($rst,'JSON');
		
	}
	
    /**
     * 修改密码提交
     * @author huajie <banhuajie@163.com>
     */
    public function profile(){
		if ( !is_login() ) {
			$this->error( '您还没有登陆',U('Public/login') );
		}
        if ( IS_POST ) {
            //获取参数
            $uid        =   is_login();
            $password   =   I('post.old');
            $repassword = I('post.repassword');
            $data['password'] = I('post.password');
            empty($password) && $this->error('请输入原密码');
            empty($data['password']) && $this->error('请输入新密码');
            empty($repassword) && $this->error('请输入确认密码');

            if($data['password'] !== $repassword){
                $this->error('您输入的新密码与确认密码不一致');
            }

            $Api = new UserApi();
            $res = $Api->updateInfo($uid, $password, $data);
            if($res['status']){
                $this->success('修改密码成功！');
            }else{
                $this->error($res['info']);
            }
        }else{
            $this->display();
        }
    }

}
